home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / mflms101.arc / INPATH.C < prev    next >
C/C++ Source or Header  |  1989-11-25  |  4KB  |  128 lines

  1. /*
  2. **                INPATH UTILITY
  3. ** This is a program similar to many "filefind" programs, except that
  4. ** it works like the UNIX "whereis" command.  The command line argument
  5. ** (only one, please) is taken to be the name of an executable
  6. ** program, and is searched under extensions .BAT, .COM, and .EXE
  7. ** for the first occurance of the program.  The search starts
  8. ** in the current directory, and if not found, procedes using the
  9. ** PATH environment variable (if there is one).  The program reports the
  10. ** first occurance of the specified file, just as it would be found
  11. ** by DOS if executed.
  12. **
  13. ** The input filename need not contain an extension, since the
  14. ** extension is discarded anyway.
  15. **
  16. ** Version 1.05  for Turbo-C 2.0
  17. ** 11-08-88 A
  18. **
  19. **  Copyright 1988-89 by Steven E. Margison
  20. **
  21. **   Modified 1989 by Bob Stout
  22. **
  23. **   As distributed, this program requires (for compilation):
  24. **     "The MicroFirm Function LIbrary for MS/QC"
  25. **   which may be obtained without registration from many Bulletin
  26. **   Board Systems.
  27. **
  28. **   or by registration:
  29. **      $25 for Docs, C, S, M, L, H libraries, and complete library source
  30. **              in C and Assembler
  31. **     MicroFirm
  32. **     P.O. Box 428
  33. **     Alief, TX 77411
  34. **
  35. */
  36.  
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <mflfiles.h>
  41.  
  42. char paths[256],
  43.      fullname[100],
  44.      dirname[100],
  45.      batname[14],
  46.      comname[14],
  47.      exename[14],
  48.      fname[14];
  49. int current;
  50.  
  51. main(argc, argv)
  52. int argc;
  53. char *argv[];
  54. {
  55.         int i, j, k;
  56.         char c;
  57.  
  58.         if(argc != 2)
  59.                 usage();
  60.         strcpy(fname, argv[1]);   /* copy argument to fname */
  61.         newext(fname, batname, "BAT");   /* make fname.bat */
  62.         newext(fname, comname, "COM");   /* make fname.com */
  63.         newext(fname, exename, "EXE");   /* make fname.exe */
  64.         dirname[0] = NUL;
  65.         current = TRUE;
  66.         findpgm();
  67.         current = FALSE;
  68.         if((j = getpath(paths)) == 0)
  69.                 notfound();
  70.         k = 0;
  71.         while(j > 0)
  72.         {
  73.                 i = 0;
  74.                 while(paths[k] != NUL)
  75.                 {
  76.                         c = paths[k];      /* save for slash check */
  77.                         dirname[i++] = paths[k++];
  78.                         --j;
  79.                 }
  80.                 if (c != '\\')
  81.                         dirname[i++] = '\\';
  82.                 dirname[i] = NUL;
  83.                 ++k;      /* position to next charcter in paths[] */
  84.                 --j;
  85.                 findpgm();
  86.         }
  87.         notfound();
  88. }
  89.  
  90. findpgm()
  91. {
  92.         strcpy(fullname, dirname);   /* test for .com file */
  93.         strcat(fullname, comname);
  94.         if(exists(fullname))
  95.                 goto found;
  96.  
  97.         strcpy(fullname, dirname);
  98.         strcat(fullname, exename);   /* test for .exe file */
  99.         if(exists(fullname))
  100.                 goto found;
  101.  
  102.         strcpy(fullname, dirname);
  103.         strcat(fullname, batname);   /* test for .bat file */
  104.         if(exists(fullname))
  105.                 goto found;
  106.  
  107.         return(FALSE);         /* file not found */
  108.  
  109. found:  if(current)
  110.                 puts("File found in current directory");
  111.         fputs(fullname, stdout);
  112.         fputc('\n', stdout);
  113.         exit(0);
  114.         return(FALSE);     /* to eliminate a compiler warning */
  115. }
  116.  
  117. notfound()
  118. {
  119.         error("File not found in PATH");
  120. }
  121.  
  122. usage()
  123. {
  124.         fputs("INPATH Version 1.10\n", stderr);
  125.         fputs("Copyright 1988-89, Steven E. Margison\n", stderr);
  126.         error("usage:  inpath <filename>");
  127. }
  128.